Skip to content

Conversation

@roland-ruedenauer
Copy link
Contributor

No longer modify the Jinja environment globals with each request processing. Doing otherwise caused issues with objects not being released as expected (e.g. "clock") because they were still being referenced somewhere in cached content created by the rendering engine.

Environment globals should not be changed after loading any templates, and template globals should not be changed at any time after loading the template. Changing globals after loading a template will result in unexpected behavior as they may be shared between the environment and other templates.

See https://jinja.palletsprojects.com/en/stable/api/#the-global-namespace

A context procesor is used instead to inject common variables to the rendering engine. Template import statements must specify "with context" when accessing context variables like e.g. "gen".

@roland-ruedenauer
Copy link
Contributor Author

roland-ruedenauer commented Oct 18, 2025

To observe the problematic behaviour with the existing code you could modify function teardown_wiki at https://github.com/moinwiki/moin/blob/master/src/moin/app.py#L389 and inspect the log output when rendering a wiki page.

import gc

...

def teardown_wiki(response):
   ...
        clock = flaskg.pop("clock", None)
        if clock is not None:
            clock.stop("total", comment=request_path)
            gc.collect()
            referrers = gc.get_referrers(flaskg.clock)
            rc = sys.getrefcount(flaskg.clock)
            logging.debug(f"obj: rc is { rc }")
            logging.debug(f"obj: referrers are { referrers }")
            del clock
   ...

No longer modify the Jinja environment globals with each request processing.
Doing otherwise caused issues with objects not being released as expected (e.g. "clock") because they were still being referenced somewhere in cached content created by the rendering engine.

> Environment globals should not be changed after loading any templates, and template globals should not be changed at any time after loading the template. Changing globals after loading a template will result in unexpected behavior as they may be shared between the environment and other templates.

See <https://jinja.palletsprojects.com/en/stable/api/#the-global-namespace>

A context procesor is used instead to inject common variables to the rendering engine.
Template import statements must specify "with context" when accessing context variables like e.g. "gen".
@roland-ruedenauer roland-ruedenauer force-pushed the template-rendering-corrections branch from 2b1c7df to b9dd430 Compare October 18, 2025 21:18
@roland-ruedenauer roland-ruedenauer marked this pull request as ready for review October 18, 2025 21:30
@RogerHaase RogerHaase merged commit 75459cf into moinwiki:master Oct 24, 2025
9 checks passed
@roland-ruedenauer roland-ruedenauer deleted the template-rendering-corrections branch October 24, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants